package test.algos.sorting.mergesort;

import static test.algos.sorting.input.InputData.INPUT_DATA;

import java.util.List;

import test.algos.sorting.Sort;

public class MergeSort implements Sort {

	private List<Integer> inputData;

	public MergeSort() {
		inputData = INPUT_DATA;
	}

	public MergeSort(List<Integer> inputData) {
		this.inputData = inputData;
	}

	@Override
	public void sort() {

		System.out.println("Before Sort:" + inputData);

		DivideStep divideStep = new DivideStep(2);

		List<Integer> curInput = inputData;

		Integer invCount = 0;

		for (int subListSize = 1; subListSize < curInput.size(); subListSize = divideStep
				.getNextSublistSize(subListSize)) {
			MergeStep mergeStep = new MergeStep(subListSize, curInput);

			MergeOutput<Integer> mergeOutput = mergeStep.merge();
			curInput = mergeOutput.getSortedList();

			invCount += mergeOutput.getInversionCount();
		}

		System.out.println("After Sort:" + curInput);

		System.out.println("Inversion Count:" + invCount);

	}

}
